﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SARUserControls.ChangJingCanshu;
using SARUserControls.ChangJingCanshu.DialogFrames;
using System.IO;
using System.Windows.Markup;

namespace SARUserControls
{
    /// <summary>
    /// 场景左边的菜单项目
    /// </summary>
    public partial class ObjectMenuItem : UserControl
    {
        public ObjectMenuItem()
        {
            this.InitializeComponent();
        }

        private string objectName;

        /// <summary>
        /// 对象名称
        /// </summary>
        public string SARObjectName
        {
            get { return objectName; }
            set { objectName = value; }
        }


        private string strSource;

        /// <summary>
        /// 图标地址
        /// </summary>
        public string SARImageSource
        {
            get
            {
                return strSource;
            }
            set
            {
                strSource = value;
                this.imgIcon.Source = new BitmapImage(new Uri(value, UriKind.Relative));
            }
        }

        private string strAttributePageType;
        private Type pageType;

        /// <summary>
        /// 属性设置页面的类型
        /// </summary>
        public string SARAttributePageType
        {
            get { return strAttributePageType; }
            set
            {
                strAttributePageType = value;
                pageType = Type.GetType(value);
                DialogPageBase page = (DialogPageBase)Activator.CreateInstance(pageType);
                this.defualtAttrValues = page.getDefaultValues();
                settingDefualtArg.IsEnabled = true;
            }
        }

        private string strTranformerType;
        private Type tranformerType;

        /// <summary>
        /// 属性转换器的地址
        /// </summary>
        public string SARTranformerType
        {
            get { return strTranformerType; }
            set
            {
                strTranformerType = value;
                tranformerType = Type.GetType(value);
            }
        }

        private void gridMask_MouseEnter(object sender, MouseEventArgs e)
        {
            this.gridMask.Opacity = 0.1;
        }

        private void gridMask_MouseLeave(object sender, MouseEventArgs e)
        {
            this.gridMask.Opacity = 0;
        }

        private void settingArgs_Click(object sender, RoutedEventArgs e)
        {
            DialogPageBase page =  new CanshuPage();
            IDictionary<string, object> values = new Dictionary<string, object>();
            values.Add("hengxiangdianshu", hengxiangdianshu);
            values.Add("zongxiangdianshu", zongxiangdianshu);
            values.Add("hengxiangjianju", hengxiangjianju);
            values.Add("zongxiangjianju", zongxiangjianju);
            page.setValues(values);
            SARDialog dialog = new SARDialog(page);
            dialog.OKClick += new OKClickHandler(dialog_OKClick);
            dialog.ShowDialog();
        }

        private int hengxiangdianshu = 1;
        private int zongxiangdianshu = 1;
        private double hengxiangjianju = 0;
        private double zongxiangjianju = 0;

        void dialog_OKClick(IDictionary<string, object> values)
        {
            this.hengxiangdianshu = Int32.Parse(values["hengxiangdianshu"].ToString());
            this.zongxiangdianshu = Int32.Parse(values["zongxiangdianshu"].ToString());
            this.hengxiangjianju = Double.Parse(values["hengxiangjianju"].ToString());
            this.zongxiangjianju = Double.Parse(values["zongxiangjianju"].ToString());
        }

        private IDictionary<string, object> defualtAttrValues;

        /// <summary>
        /// 设置或者获取默认属性
        /// </summary>
        public IDictionary<string, object> SARDefualtAttrValues
        {
            get { return defualtAttrValues; }
            set { defualtAttrValues = value; }
        }

        private void settingDefualtArg_Click(object sender, RoutedEventArgs e)
        {
            DialogPageBase page = (DialogPageBase)Activator.CreateInstance(pageType);
            page.setValues(this.defualtAttrValues);
            SARDialog dialog = new SARDialog(page);
            dialog.OKClick += new OKClickHandler(dialog_OKClick1);
            dialog.ShowDialog();
        }
        void dialog_OKClick1(IDictionary<string, object> values)
        {
            defualtAttrValues = values;
        }

        private Point startPoint;
        private void gridMask_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition(null);
        }

        private void gridMask_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = startPoint - mousePos;

            if (e.LeftButton == MouseButtonState.Pressed &&
                (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            {
                List<ObjectInstance> instances = new List<ObjectInstance>();
                for (int i = 0; i < this.hengxiangdianshu; i++)
                {
                    for (int j = 0; j < this.zongxiangdianshu; j++)
                    {
                        ObjectInstance newInstance = new ObjectInstance();
                        newInstance.SARObjectName = this.SARObjectName;
                        newInstance.SARImageSource = this.SARImageSource;
                        newInstance.SARRealHeight = ((ObjectScene)this.Tag).SARRealheight;
                        newInstance.SARRealWidth = ((ObjectScene)this.Tag).SARRealwidth;
                        newInstance.SARAttrValues = new Dictionary<string, object>();
                        newInstance.SARTranformerType = this.tranformerType;

                        //如果配置了pageType，才有可能有属性
                        if (this.pageType != null)
                        {
                            newInstance.SARPageType = this.pageType;
                            foreach (string key in this.defualtAttrValues.Keys)
                            {
                                newInstance.SARAttrValues.Add(key, this.defualtAttrValues[key]);
                            }
                        }

                        

                        double x = i * hengxiangjianju - (hengxiangdianshu - 1) * hengxiangjianju / 2;
                        double y = j * zongxiangjianju - (zongxiangdianshu - 1) * zongxiangjianju / 2;

                        newInstance.SARPostion = new Point(x, y);

                        instances.Add(newInstance);
                    }
                }

                DataObject dragData = new DataObject(instances);

                DragDrop.DoDragDrop(this, instances, DragDropEffects.Move);
            }
        }       
    }
}